package GameLogic
{
	import BasicEngine.Dispatcher;
	import BasicEngine.Vector2D;
	import DataStore.BmpDataPool;
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.Loader;
	import flash.display.Shape;
	import flash.display.SpreadMethod;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	import flash.utils.Dictionary;
	import LoadUnit.LoadManager;
	import luaAlchemy.LuaAlchemy;
	import RobotPlayer.BaseRobot;
	import RobotPlayer.RobotFactory;
	import SoundManager.SoundFactory;
	
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	public class Game
	{
		/**
		 * 主容器
		 */
		public static var MainCanvas:Sprite;
		/**
		 * 地图容器
		 */
		public static var Map:Sprite;
		/**
		 * 菜单容器
		 */
		public static var MenuCanvas:Sprite;
		/**
		 * Player容器
		 */
		public static var PlayerCanvas:Sprite;
		/**
		 * 唯一的主角
		 */
		public static var LeadCanvas:Sprite;
		/**
		 * 建筑容器
		 */
		public static var Building:Sprite;
		/**
		 * 天空容器
		 */
		public static var SkyCanvas:Sprite;
		
		public static var MainTimer:Timer;			
		public static var Pause:Boolean;
		public static var Width:int;
		public static var Height:int;		
		public static var MainStage:Stage;
		
		public static var NEXT_ID:int = 1000;
        public static var LuaAl:LuaAlchemy;		
		public static var LuaDict:Dictionary = new Dictionary();
		public static var GraphicsTool:Graphics;
		
		public static var ResourceReady:Boolean = false;
		public static var OpenLuaEditBtn:SpriteBtn;
		public static var LuaEdit:LuaEditer;
		public static var GoAndPlayBtn:PauseBtn;
		public static var InfoShowBtn:InfoShow;
		public static var PrintInfo:TraceWin;
		public static var Robothead:Head;
		public static var SetButton:SettingButton;
		public static var FpsShow:FpsCounter;
		
		public static var Tooltip:TextField = new TextField();
		
		public static var ShowRadar:LargeRadar;
		/**
		 * 打开编辑器之前记录一下雷达的开启状态
		 */
		public static var InitShowRadar:Boolean;
		
		public static var MissileNextId:int = 10000;
		
		public static var AddMissileNum:int = 0;
		public static var RemovMissileNum:int = 0;
		
		public static var LuaScriptApiSt:Dictionary;
		
		public function Game()
		{
		}
		
		//初始化
		public static function Init(_main:Sprite,_graphics:Graphics,_width:int,_height:int):void
		{
			Dispatcher.Init();
			InitCanvas(_main);
			RobotFactory.Init();
			LuaAl = new LuaAlchemy();
			LuaAl.setGlobal("this", _main);
			Pause = true;
			Width = _width;
			Height = _height;
			GraphicsTool = _graphics;
			MainTimer = new Timer(20,0); //20 毫秒计时一次
			MainTimer.addEventListener(TimerEvent.TIMER, function(e:TimerEvent) :void
			{
				Update();
			});		
			Tooltip.backgroundColor = 0x333333;
			Tooltip.background = true;
			Tooltip.selectable = false;
			LuaScriptApiSt = new Dictionary();
		}
		
		
		public static function Update():void
		{
			if (!Pause)
			{
				Dispatcher.ExecuteUpdate();
				LoadManager.Update();
				
				var i:int = 0;
				for each(var item:BaseRobot in RobotFactory.RobotRecords)
				{
					i++;
				}
				RobotFactory.RobotNums = i;
			}
		}
		
		//游戏启动，启动游戏调度,启动循环
		public static function Star():void
		{
			MainTimer.start();
			Pause = false;
		}
		
		public static function WorldToLocal(_global:Vector2D,_localCoordinatorOrigin:Vector2D,_localCoordinatorHead:Vector2D):Vector2D
		{
			var local:Vector2D =  new Vector2D(_global.X - _localCoordinatorOrigin.X, _global.Y - _localCoordinatorOrigin.Y);
			var angle:Number = local.Angle - (_localCoordinatorHead.Angle - Math.PI/2);
			var re:Vector2D = new Vector2D(0, 0);
			re.Length = local.Length;
			re.Angle = angle;
			return re;
		}
		
		public static function LocalToWorld(_local:Vector2D, _localCoordinatorOrigin:Vector2D, _localCoordinatorHead:Vector2D):Vector2D
		{
			var v:Vector2D = _local.Clone();
			v.Angle = _local.Angle + _localCoordinatorHead.Angle;
			v.X = v.X + _localCoordinatorOrigin.X;
			v.Y = v.Y + _localCoordinatorOrigin.Y;
			return v;
		}
		
		public static function ImgResourceReady():void
		{
			ResourceReady = true;
		}
		
		public static function Print(_st:String):void
		{
			LuaEdit.OutputWin.text += _st;
		}
		
		public static function GetSystemTime():int
		{
			return Dispatcher.Clock.SystemTime;
		}
		
		public static function GetTick():int
		{
			return Dispatcher.Clock.Tick;
		}
		
		public static function SetRobotTarget(_x:Number, _y:Number):void
		{
			var ro:BaseRobot = RobotFactory.RobotRecords[1001] as BaseRobot;
			ro.SetTarget(_x, _y);
		}
		
		public static function GetForce(_length:Number, _angle:Number):Vector2D
		{
			var v:Vector2D = new Vector2D(1, 1).Normalize();
			v.Length = _length;
			v.Angle = _angle;
			return v;
		}
		
		public static function AddTooltip(_canvas:Sprite,_obj:DisplayObject,_note:String,_x:int,_y:int):void
		{
			Tooltip.text = _note;
			_canvas.addChild(Game.Tooltip);
			Tooltip.x = _obj.x + _x;
			Tooltip.y = _obj.y + _y;
			Tooltip.visible = true;
			Tooltip.textColor = 0xffffff;
			Tooltip.width = Tooltip.text.length * 13;
			Tooltip.height = 22;
			Mouse.cursor = MouseCursor.BUTTON;
		}
		
		public static function RemoveTooltip():void
		{
			Tooltip.text = "";
			Tooltip.visible = false;
			Mouse.cursor = MouseCursor.AUTO;
		}
		
		public static function DesignUI():void
		{
			LuaEdit = new LuaEditer();
			LuaEdit.visible = false;
			OpenLuaEditBtn =  new SpriteBtn(BmpDataPool.LuaeditPool[4019]);
			//MenuCanvas.addChild(OpenLuaEditBtn);
			OpenLuaEditBtn.addEventListener(MouseEvent.CLICK, function(e:Event):void
			 {
				var l:Dictionary = SoundFactory.Sounds;
				SoundFactory.Sounds["button2"].play();
				LuaEdit.visible = true;
				RemoveTooltip();
				InitShowRadar = SetButton.SettingPanel.radar.selected;
				SetButton.SettingPanel.radar.selected = false;
			 });
			OpenLuaEditBtn.addEventListener(MouseEvent.MOUSE_OVER, function(e:Event):void
			 {
			   	Mouse.cursor = MouseCursor.BUTTON;
				if (! LuaEdit.visible)
				{
				    AddTooltip( OpenLuaEditBtn,  OpenLuaEditBtn.Img, "打开Lua脚本编辑器", 10, -30);
				}
			});
			OpenLuaEditBtn.addEventListener(MouseEvent.MOUSE_OUT, function(e:Event):void
			{
				Mouse.cursor = MouseCursor.AUTO;
				RemoveTooltip();
			});
			OpenLuaEditBtn.x = 555;
			OpenLuaEditBtn.y = 540;
			
			
			var bottomeBorder:Bitmap = new Bitmap(BmpDataPool.LuaeditPool[4000]);
			var img:flash.display.BitmapData
			bottomeBorder.y =  Height - 18;
			MenuCanvas.addChild(bottomeBorder);
			MenuCanvas.setChildIndex(bottomeBorder, 0);
			var bottomeBox:Bitmap = new Bitmap(BmpDataPool.LuaeditPool[4050]);
			bottomeBox.y =  Height - bottomeBox.height;
			bottomeBox.x = -4;
			 MenuCanvas.addChild(bottomeBox);
			
			 GoAndPlayBtn = new PauseBtn(BmpDataPool.LuaeditPool[4040], BmpDataPool.LuaeditPool[4041], false);
			 MenuCanvas.addChild( GoAndPlayBtn);
			 GoAndPlayBtn.x = 10;
			 GoAndPlayBtn.y = 474;
			
			 InfoShowBtn = new InfoShow(BmpDataPool.LuaeditPool[4017], BmpDataPool.LuaeditPool[4018],BmpDataPool.LuaeditPool[4019], false);
			 MenuCanvas.addChild( InfoShowBtn);
			 InfoShowBtn.x = 715;
			 InfoShowBtn.y = 540;
			
			 PrintInfo = new TraceWin();
			 MenuCanvas.addChild( PrintInfo);
			
			 Robothead = new Head();
			 MenuCanvas.addChild( Robothead);
			 Robothead.x = 74;
			 Robothead.y = 508; 
			 MenuCanvas.setChildIndex( Robothead, 0);
			
			 SetButton = new SettingButton();
			 MenuCanvas.addChild( SetButton);
			 SetButton.x = 71;
			 SetButton.y = 437;
			 MenuCanvas.setChildIndex( SetButton, 0);
			
			 ShowRadar = new LargeRadar();
			 MenuCanvas.addChild( ShowRadar);
			 ShowRadar.x = 760 - 165;
			
			 FpsShow = new FpsCounter();
			 MenuCanvas.addChild( FpsShow);
			 FpsShow.x = 10;
			 FpsShow.y = 30;
			
			//var line:Shape = new Shape();
			//line.graphics.lineStyle(5, 0xff0000, 1);
			//line.graphics.drawCircle(0, 0, 4);
			//line.graphics.moveTo(0, 0);
			//line.graphics.lineTo(200, 0);
			 //PlayerCanvas.addChild(line);
			//line.x = 300;
			//line.y = 300;
			//line.rotation = -270;
			
			var t:TextField = new TextField();
			//MenuCanvas.addChild(t);
			t.text = "   QQ:474165841  蜂窝煤  新浪微博：@向恺然";
			t.width = 280;
			t.height = 20;
			t.background = true;
			t.x = 158;
			t.y = 560;
			t.backgroundColor = 0xbbbbbb;
			
			var timer:ShowTime = new ShowTime();
			MenuCanvas.addChild(timer);
			timer.x = 30;
			//timer.y = 164;
			timer.scaleX = timer.scaleY = 0.9;
			timer.alpha = 0.9;
		}
		
		/**
		 * 初始化容器
		 */
		private static function InitCanvas(_root:Sprite):void
		{
			MainCanvas = _root;
		    Map = new Sprite();
			MenuCanvas = new Sprite();
			PlayerCanvas = new Sprite();
			LeadCanvas = new Sprite();
			Building = new Sprite();
			SkyCanvas = new Sprite();
			
			MainCanvas.addChild(Map);
			MainCanvas.addChild(PlayerCanvas);
			MainCanvas.addChild(LeadCanvas);
			MainCanvas.addChild(Building);
			MainCanvas.addChild(SkyCanvas);
			MainCanvas.addChild(MenuCanvas);
		}
	}
}